package com.op.teachingliteapp.activity;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.op.teachingliteapp.R;
import com.op.teachingliteapp.fragment.SendNotifFragment;
import com.op.teachingliteapp.fragment.SendPostFragment;
import com.op.teachingliteapp.fragment.notif.NotifFragment;
import com.op.teachingliteapp.fragment.resource.ResouceFragment;
import com.op.teachingliteapp.fragment.seminar.SeminarFragment;
import com.op.teachingliteapp.sender.Runtime;
import com.op.teachingliteapp.sender.ui.view.device.DeviceScanDialog;
import com.op.teachingliteapp.util.CommandSendUtil;
import com.op.teachingliteapp.util.ScreenSendUtils;

/*
 * 这是第二个界面的主Activity
 */
public class CourseActivity extends Activity {
	private static final String TAG = CourseActivity.class.getSimpleName();
	private TextView function;//顶栏中间的显示
	private TextView courseName;//返回键旁边的
	private String topTextview="";
	public  static int muenMun=0;
	private ImageButton more;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_course);

		function = (TextView) findViewById(R.id.function);
		function.setText("教学资料");
		Bundle mbundle= getIntent().getExtras();
		courseName= (TextView) findViewById(R.id.courseName);
		courseName.setText(mbundle.getString("courseName"));

		more=(ImageButton) findViewById(R.id.more);
		more.setVisibility(View.GONE);

		if (getFragmentManager().findFragmentById(R.id.main_show) == null) {
			switchFragment(new ResouceFragment());
		}

		// 返回
		findViewById(R.id.back).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				onBackPressed();
			}
		});
		courseName.findViewById(R.id.courseName).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				onBackPressed();
			}
		});

		// 教学资料
		findViewById(R.id.button_data).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				findViewById(R.id.button_data).setBackgroundResource(R.drawable.button_data1);
				findViewById(R.id.button_discussion).setBackgroundResource(R.drawable.button_discussion3);
				findViewById(R.id.button_notice).setBackgroundResource(R.drawable.button_notice3);
				switchFragment(new ResouceFragment());
				function.setText("教学资料");
				topTextview="教学资料";
				more.setVisibility(View.GONE);
			}
		});

		// 课程讨论
		findViewById(R.id.button_discussion).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				findViewById(R.id.button_data).setBackgroundResource(R.drawable.button_data3);
				findViewById(R.id.button_discussion).setBackgroundResource(R.drawable.button_discussion1);
				findViewById(R.id.button_notice).setBackgroundResource(R.drawable.button_notice3);
				Toast.makeText(getApplicationContext(), "课程讨论", Toast.LENGTH_SHORT).show();
				function.setText("课程讨论");
				topTextview="课程讨论";
				switchFragment(new SeminarFragment());
				muenMun=1;
				more.setVisibility(View.VISIBLE);

			}
		});

		// 课程通知
		findViewById(R.id.button_notice).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				findViewById(R.id.button_data).setBackgroundResource(R.drawable.button_data3);// 杩樺師
				findViewById(R.id.button_discussion).setBackgroundResource(R.drawable.button_discussion3);// 杩樺師
				findViewById(R.id.button_notice).setBackgroundResource(R.drawable.button_notice1);// 鎸変笅
				Toast.makeText(getApplicationContext(), "课程通知", Toast.LENGTH_SHORT).show();
				function.setText("课程通知");
				topTextview="课程通知";
				switchFragment(new NotifFragment());
				muenMun=2;
				more.setVisibility(View.VISIBLE);
			}
		});

		// 投影
		// -------------------------------------------------------------------------------
		ImageButton sendShow = (ImageButton) findViewById(R.id.send_show);
		Button deviceScanBtn = (Button) findViewById(R.id.device_scan);
		RadioGroup radioGroup = (RadioGroup) findViewById(R.id.openOrclose);
		Runtime.refreshStatus(sendShow, deviceScanBtn, radioGroup);
		sendShow.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (Runtime.isOffLine()) {

					new DeviceScanDialog(CourseActivity.this).show();

				} else {
					if (Runtime.isSend()) {
						Runtime.pause();
						Runtime.refreshStatus();

					} else {

						Runtime.play();
						Runtime.refreshStatus();
						ScreenSendUtils.send();
					}

				}
			}
		});

		radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				if (Runtime.getIp() != null) {
					switch (checkedId) {
					case R.id.openStudent:
						CommandSendUtil.openStudentPort(CourseActivity.this);
						break;
					case R.id.closeStudent:
						CommandSendUtil.closeStudentPort(CourseActivity.this);
						break;
					}
				}
			}

		});

		deviceScanBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				new DeviceScanDialog(CourseActivity.this).show();

			}
		});

		// 更多
		more.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				PopupMenu	popupMenu = new PopupMenu(CourseActivity.this, v);
				switch (muenMun) {
				case 1:
					popupMenu.getMenuInflater().inflate(R.menu.seminar, popupMenu.getMenu());
					break;
				case 2:
					popupMenu.getMenuInflater().inflate(R.menu.notification, popupMenu.getMenu());
					break;
				case 3:
					popupMenu.getMenuInflater().inflate(R.menu.son_seminar, popupMenu.getMenu());
					break;
				default:
					break;
				}
				popupMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {

					@Override
					public boolean onMenuItemClick(MenuItem item) {
						switch (item.getItemId()) {
						case R.id.sendpost:
							function.setText("发送帖子");
							getFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.main_show2, new SendPostFragment()).commit();
							break;
						case R.id.send_notification:
							function.setText("发送通知");
							getFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.main_show2, new SendNotifFragment()).addToBackStack(null).commit();
							break;
						default:
							break;
						}
						return false;
					}
				});
				popupMenu.show();        		
			}
		});


	}

	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onPostResume() {
		super.onPostResume();
		Log.i("CourseActivity", "onPostResume");
	}

	/*
	 * 清空堆栈再添加fragment
	 */
	private void switchFragment(Fragment fragment) {
		getFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);// 清空堆栈
		getFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.main_show2, fragment).commit();

	}

	@Override
	public void onBackPressed() {
		/*
		 * 判断堆栈是否只有一个
		 */
		if (getFragmentManager().getBackStackEntryCount() == 1) {
			finish();
			Runtime.pause();
			return;
		}
		/*
		 * 不是一个的调用系统的onBackPressed
//		 */
		if(topTextview.equals("课程通知")||topTextview.equals("课程讨论")||topTextview.equals("教学资料")){
			function.setText(topTextview);
			if(topTextview.equals("课程讨论"))
			{
				muenMun=2;
			}
			if(topTextview.equals("教学资料")){
				muenMun=1;
			}
			Log.i(TAG, "******/*/*/*/*/*/*/*");
		}
		super.onBackPressed();
	}
}
